//
//  DrugDetailViewController.m
//  MedicineBox
//
//  Created by dllo on 15/8/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "DrugDetailViewController.h"
#import "AsyncConnection.h"
#import "HomeModel.h"
#import "DrugDetailHeaderView.h"
#import "DrugDetailTableViewCell.h"

#import "MedicineBoxDataBase.h"
#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"
#import "UMSocial.h"

@interface DrugDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate,UMSocialDataDelegate,DrugDetailTableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) HomeModel *model;
@property (nonatomic, assign) CGFloat height;

@end

@implementation DrugDetailViewController

-(void)dealloc
{
    [_tableView release];
    [_model release];
    [_namecn release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
         self.model = [[HomeModel alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
    [self handleData];
    [self createNavigation];
    [self createTableView];
    
}

#pragma mark -
#pragma mark 设置导航栏
- (void)createNavigation
{
    
    
    
    self.model.type = @"her";
//标题
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    navView.backgroundColor = [UIColor colorWithRed:86/255.0 green:137/255.0 blue:156/255.0 alpha:1];
    navView.nightBackgroundColor = UIColorFromRGB(0x444444);
    [self.view addSubview:navView];
    [navView release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100) / 2, 30, 100, 30)];
    titleLabel.text = @"详细信息";
    titleLabel.font = [UIFont fontWithName:@"Baskerville-SemiBold" size:20];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.nightTextColor = [UIColor whiteColor];
    [navView addSubview:titleLabel];
    [titleLabel release];
//返回按钮
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10 , 30, 60, 30)];
    [navView addSubview:backButton];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton release];
//分享
    
    UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 40 , 30, 20, 20)];
    [shareButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shareButton" ofType:@"png"]] forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareButton];
    [shareButton release];
    
//收藏
    
    UIButton *collectButton = [[UIButton alloc] initWithFrame:CGRectMake(shareButton.frame.origin.x - 40, 30, 20, 20)];
    HomeModel *collectModel = [MedicineBoxDataBase selectOnlyWith:[NSString stringWithFormat:@"%@",self.deatilId]];
    if (collectModel) {
        [collectButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"collectSuccess" ofType:@"png"]] forState:UIControlStateNormal];
    }else
    {
        [collectButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cButton" ofType:@"png"]] forState:UIControlStateNormal];
    }
    [collectButton addTarget:self action:@selector(collectAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:collectButton];
    [collectButton release];
    
}
//收藏点击事件
- (void)collectAction:(UIButton *)button
{
    if ([MedicineBoxDataBase insertWithModel:self.model]) {
        
       
        [button setBackgroundImage:[UIImage imageNamed:@"collectSuccess.png"] forState:UIControlStateNormal];
    }else
    {
        [button setBackgroundImage:[UIImage imageNamed:@"cButton.png"] forState:UIControlStateNormal];
        [MedicineBoxDataBase deleteWith:self.model];
    }
}

//分享点击事件
- (void)shareAction:(UIButton *)button
{
#pragma mark -
#pragma mark 截屏图片
    UIGraphicsBeginImageContext(self.view.bounds.size);     //currentView 当前的view  创建一个基于位图的图形上下文并指定大小为
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
    UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图

    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55e0ffc5e0f55a9a44002606"
                                      shareText:@"你要分享的文字"
                                     shareImage:viewImage
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,UMShareToSms,nil]
                                       delegate:self];
}

- (void)didFinishGetUMSocialDataResponse:(UMSocialResponseEntity *)response
{
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [UMSocialSnsService  applicationDidBecomeActive];
    
}


//返回点事件
- (void)buttonAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -
#pragma mark 数据处理

- (void)handleData
{
   
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@"app" forKey:@"app_key"];
    [dic setValue:self.deatilId forKey:@"id"];
    [dic setValue:@"9DFAAD5404FCB6168EA6840DCDFF39E5" forKey:@"sign"];
    NSString *str = [NSString stringWithFormat:@"%@", self.deatilId];
    [AsyncConnection asyncConnectionWithUrl:MB_DrugDetail name:str parameters:dic block:^(id data) {
        
        if ([data objectForKey:@"results"] != nil) {
            NSDictionary *results = [data objectForKey:@"results"];
            [self.model setValuesForKeysWithDictionary:results];
            [_model release];
            [self.tableView reloadData];
            
        }
        
    } get:@"get"];
}

#pragma mark -
#pragma mark 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.nightBackgroundColor = UIColorFromRGB(0x343434);
    self.tableView.nightSeparatorColor = UIColorFromRGB(0x313131);
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }else
    {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrugDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[[DrugDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
    }
    cell.drugDetailDelegate = self;
    [cell valueWithModel:self.model indexPath:indexPath];
    cell.nameStr = self.namecn;
    return cell;
}

#pragma mark -
#pragma mark HeaderView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        DrugDetailHeaderView *view = [[DrugDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
        [view valueWithModel:self.model];
        view.name = self.namecn;
        view.score = self.score;
        return view;
    }else
    {
        return nil;
    }
}

- (void)bringHeightToController:(CGFloat)height path:(NSIndexPath *)indexPath
{

    self.height = height;
    [self tableView:self.tableView heightForRowAtIndexPath:indexPath];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 120;
    }else
    {
        return 50;
    }
}
//索引
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"说明书";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark -
#pragma mark 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//   NSString *text = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@%@%@%@",self.model.composition, self.model.drugattribute, self.model.gongneng, self.model.usage, self.model.adr, self.model.contraindication, self.model.note, self.model.interaction, self.model.storage, self.model.shelflife, self.model.codename,self.model.standard, self.model.specificationdate,self.model.refdrugcompanyname];
//    
//    return [[DrugDetailTableViewCell class] heightWithText:text] + 550 + 100;
    
    return self.height + 330;
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end
